//
//  DDTMainTabBar.swift
//  weibo
//
//  Created by mac on 16/9/20.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class DDTMainTabBar: UITabBar {
    
    var composeClosure: (() ->())?
    
    
    //懒加载UIButton
    lazy var composeBtn: UIButton = {
        
        let MiddleBtn = UIButton()
        MiddleBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        MiddleBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        MiddleBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        MiddleBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        
        self.addSubview(MiddleBtn)
        
        MiddleBtn.addTarget(self, action:"composeClick", forControlEvents:.TouchUpInside)
        
        
        return MiddleBtn
    }()
    
    //tabBar总数
    let totalTabBarItem = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = bounds.width / CGFloat(totalTabBarItem)
        var index = 0
        
        for item in subviews {
            
            let cls = NSClassFromString("UITabBarButton")!
            if item.isKindOfClass(cls) {
                item.frame = CGRectMake(CGFloat(index) * width, 0, width, self.bounds.height)
                index += 1
                
                if index == 2 {
                    composeBtn.frame = CGRectMake(CGFloat(index) * width, 0, width, bounds.height)
                    index += 1
                }
            }
            
        }
        
        
    }
    
    
    //准备一个点击事件回调
    func composeClick() {
        composeClosure?()
    }
   
    
}
